package data

import (
    "crypto/md5"
    "fmt"
    "io"
    "math/rand"
    "strconv"
    "time"
)

// Getmd5 生成 md5 加密串
func Getmd5(prefix, str string) string {
    m5 := md5.New()
    str = prefix + str
    io.WriteString(m5, str)
    rst := fmt.Sprintf("%x", m5.Sum(nil))
    return rst
}

// GetRandomNum 获取随机数字串
func GetRandomNum(num int) (str string) {
    rand.Seed(time.Now().UnixNano())
    for i := 0; i < num; i++ {
        n := rand.Intn(9)
        str += strconv.Itoa(n)
    }
    return str
}

// GetRandomStr 获取随机字符串
func GetRandomStr(num int) (str string) {
    if num < 1 {
        num = 8
    }
    var patter = "AaZ!zBb0Y)yCc9@XxD(d8W#w7E*eVv$F6fU&uG5gTt%HhS4sIiRr^3JjQqKkP2pLlO1oMmNn"
    patterArr := []rune(patter)
    rand.Seed(time.Now().UnixNano())
    for i := 0; i < num; i++ {
        n := rand.Intn(72)
        s := patterArr[n]
        str += string(s)
    }
    return
}

// GetUnixTime 字符串时间转时间辍
func GetUnixTime(strTime string) int64 {
    loc, _ := time.LoadLocation("Local")
    theTime, err := time.ParseInLocation("2006-01-02 15:04:05", strTime, loc)
    if err != nil {
        return 0
    }
    return theTime.Unix()
}
